package com.jsmtr.rms.controller.writtentest;

import cn.ktjk.DataTableUtile.DataTable.DataRow;
import cn.ktjk.DataTableUtile.DataTable.DataTable;
import com.jsmtr.rms.entity.LimitRequest;
import com.jsmtr.rms.entity.QuestionBank;
import com.jsmtr.rms.service.ITopicService;
import com.jsmtr.rms.utils.ExcelHelper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.PrintWriter;

import static com.jsmtr.rms.entity.ResponseBuilder.simpleResponse;

/**
 * @author wyh
 */
@Controller
@RequestMapping("/TopicManager")
public class TopicManagerController {
    @Resource
    ITopicService topicService;

    @RequestMapping("/")
    public String topicManager(Model model) {
        model.addAttribute("kids", topicService.getKids());
        model.addAttribute("paths", topicService.getPaths());
        return "exam/topicBank";
    }

    @RequestMapping("/addTopicFrame/")
    public String addTopicFrame(String id, Model model) {
        if (id == null) {
            return "exam/addTopic";
        } else {
            QuestionBank questionBank = topicService.seeTopic(id);
            model.addAttribute("topic", questionBank);
            model.addAttribute("topicJson", JSONObject.fromObject(questionBank).toString());
            return "exam/editTopic";
        }
    }

    @RequestMapping(value = "/getTopicList", method = RequestMethod.POST)
    public void getTopicList(@RequestBody LimitRequest lRequest, HttpServletResponse response) {
        try {
            response.getWriter().write(topicService.getTopoicList(lRequest));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/addTopic")
    public void addTopic(@RequestBody QuestionBank topic, HttpServletResponse response, HttpServletRequest request) {
        simpleResponse(topicService.addTopic(topic, request), response);
    }

    @RequestMapping("/editTopic")
    public void editTopic(@RequestBody QuestionBank questionBank, HttpServletResponse response, HttpServletRequest request) {
        simpleResponse(topicService.editTopic(questionBank, request), response);
    }

    @RequestMapping("/deleteTopic")
    public void deleteTopic(String id, HttpServletResponse response) {
        simpleResponse(topicService.deleteTopic(id), response);
    }

    @RequestMapping("/seeTopic")
    public void seeTopic(String id, HttpServletResponse response) {
        try {
            response.getWriter().write(JSONObject.fromObject(topicService.seeTopic(id)).toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/enableTopic")
    public void enableTopic(String id, Boolean state, HttpServletResponse response, HttpServletRequest request) {
        simpleResponse(topicService.editenableTopic(id, state, request), response);
    }


    final String SingleChoose = "单项选择题";
    final String MultiChoose = "不定项选择题";
    final String Blank = "填空题";
    final String TrueOrFalse = "判断正误题";
    final String Saq = "简答题";
    final String Complex = "综合题";

    @RequestMapping("/importTopic")
    public void importTopic(MultipartFile upfile, HttpServletRequest request, HttpServletResponse response) {
        try {
            PrintWriter writer = response.getWriter();
            DataTable dataTable = ExcelHelper.ReadExcelToDataTable(upfile);
            if (dataTable != null && dataTable.Rows.size() > 0) {
                long chooseCount = dataTable.Rows.stream()
                        .skip(1)
                        .limit(dataTable.Rows.size() - 1)
                        .filter(dataRow -> SingleChoose.equals(dataRow.getValue(1)) ||
                                MultiChoose.equals(dataRow.getValue(1)))
                        .map(dataRow -> chooseAdd(dataRow, request))
                        .filter(Boolean::booleanValue)
                        .count();

                long blankCount = dataTable.Rows.stream()
                        .skip(1)
                        .limit(dataTable.Rows.size() - 1)
                        .filter(dataRow -> Blank.equals(dataRow.getValue(1)) ||
                                TrueOrFalse.equals(dataRow.getValue(1)) ||
                                Saq.equals(dataRow.getValue(1)) ||
                                Complex.equals(dataRow.getValue(1)))
                        .map(dataRow -> noChooseAdd(dataRow, request))
                        .filter(Boolean::booleanValue)
                        .count();

                writer.write((int)(chooseCount + blankCount));
            } else {
                writer.write(-1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    boolean chooseAdd(DataRow dataRow, HttpServletRequest request) {
        QuestionBank bank = new QuestionBank();
        bank.setName((String) dataRow.getValue(0));
        bank.setType((String) dataRow.getValue(1));
        bank.setKid((String) dataRow.getValue(2));
        bank.setPath((String) dataRow.getValue(3));
        bank.setDifficulty((String) dataRow.getValue(4));
        bank.setData((String) dataRow.getValue(5));
        bank.setAnswer((String) dataRow.getValue(6));
        bank.setChoose((String) dataRow.getValue(7));
        JSONArray options = new JSONArray();
        for (int i = 8; i < dataRow.size() - 1; i++) {
            options.add(dataRow.getValue(i));
        }
        bank.setOptions(options.toString());
        return topicService.addTopicFindKid(bank, request);
    }

    boolean noChooseAdd(DataRow dataRow, HttpServletRequest request) {
        QuestionBank bank = new QuestionBank();
        bank.setName((String) dataRow.getValue(0));
        bank.setType((String) dataRow.getValue(1));
        bank.setKid((String) dataRow.getValue(2));
        bank.setPath((String) dataRow.getValue(3));
        bank.setDifficulty((String) dataRow.getValue(4));
        bank.setData((String) dataRow.getValue(5));
        bank.setAnswer((String) dataRow.getValue(6));
        bank.setChoose((String) dataRow.getValue(7));
        return topicService.addTopicFindKid(bank, request);
    }
}
